import connect
client = connect.connect()

# 使用数据库test
db = client['test']

# 展示列表的所有内容
def showCollectionData(c):
    result = c.find()
    for item in result:
        print(item)
    
# 获取对应的集合（列表）
collection = db.test

# 展示内容
showCollectionData(collection)
"""
{'_id': ObjectId('6571c4f36b687d2df1c29112'), 'key': 'test'}
{'_id': ObjectId('6571c4fc6b687d2df1c29113'), 'key': 'test2'}
{'_id': ObjectId('6571c5016b687d2df1c29114'), 'key': 'test3'}
{'_id': ObjectId('6571c67d59e0c009ac38994e'), 'key': '123', 'age': 18, 'name': 'john'}
{'_id': ObjectId('6571c70ec4d0968af55f2b92'), 'key': '123', 'age': 18, 'name': 'john'}
{'_id': ObjectId('6571c70ec4d0968af55f2b93'), 'key': '456', 'age': 20, 'name': 'lucy'}
{'_id': ObjectId('6571c70ec4d0968af55f2b94'), 'key': '789', 'age': 22, 'name': 'mike'}
{'_id': ObjectId('6571c91c1aa984bb58f0dabf'), 'key': '111', 'age': 18, 'name': 'john'}
{'_id': ObjectId('6571c91c1aa984bb58f0dac0'), 'key': '222', 'age': 20, 'name': 'lucy'}
{'_id': ObjectId('6571c91c1aa984bb58f0dac1'), 'key': '333', 'age': 22, 'name': 'mike'}
"""
# 更新key为test的,把key改成test666
# $set 不存在则创建,存在则直接替换
# collection.update_one({"key":"test"}, { "$set": {"key": "test666"} })
# showCollectionData(collection)
"""
{'_id': ObjectId('6571c4f36b687d2df1c29112'), 'key': 'test666'}
{'_id': ObjectId('6571c4fc6b687d2df1c29113'), 'key': 'test2'}
{'_id': ObjectId('6571c5016b687d2df1c29114'), 'key': 'test3'}
{'_id': ObjectId('6571c67d59e0c009ac38994e'), 'key': '123', 'age': 18, 'name': 'john'}
{'_id': ObjectId('6571c70ec4d0968af55f2b92'), 'key': '123', 'age': 18, 'name': 'john'}
{'_id': ObjectId('6571c70ec4d0968af55f2b93'), 'key': '456', 'age': 20, 'name': 'lucy'}
{'_id': ObjectId('6571c70ec4d0968af55f2b94'), 'key': '789', 'age': 22, 'name': 'mike'}
{'_id': ObjectId('6571c91c1aa984bb58f0dabf'), 'key': '111', 'age': 18, 'name': 'john'}
{'_id': ObjectId('6571c91c1aa984bb58f0dac0'), 'key': '222', 'age': 20, 'name': 'lucy'}
{'_id': ObjectId('6571c91c1aa984bb58f0dac1'), 'key': '333', 'age': 22, 'name': 'mike'}
"""

# 增加, 将年龄为22的数据,增加一个height数据,值为180
collection.update_one({"age": 22}, { "$set": {"height": 180} })
showCollectionData(collection)

# 更新多个
collection.update_many({"age": 22}, { "$set": {"height": 180} })
showCollectionData(collection)
"""
{'_id': ObjectId('6571c4f36b687d2df1c29112'), 'key': 'test666'}
{'_id': ObjectId('6571c4fc6b687d2df1c29113'), 'key': 'test2'}
{'_id': ObjectId('6571c5016b687d2df1c29114'), 'key': 'test3'}
{'_id': ObjectId('6571c67d59e0c009ac38994e'), 'key': '123', 'age': 18, 'name': 'john'}
{'_id': ObjectId('6571c70ec4d0968af55f2b92'), 'key': '123', 'age': 18, 'name': 'john'}
{'_id': ObjectId('6571c70ec4d0968af55f2b93'), 'key': '456', 'age': 20, 'name': 'lucy'}
{'_id': ObjectId('6571c70ec4d0968af55f2b94'), 'key': '789', 'age': 22, 'name': 'mike', 'height': 180}
{'_id': ObjectId('6571c91c1aa984bb58f0dabf'), 'key': '111', 'age': 18, 'name': 'john'}
{'_id': ObjectId('6571c91c1aa984bb58f0dac0'), 'key': '222', 'age': 20, 'name': 'lucy'}
{'_id': ObjectId('6571c91c1aa984bb58f0dac1'), 'key': '333', 'age': 22, 'name': 'mike', 'height': 180}
"""